var mode;
var oldmatk;
var oldma;

$(document).ready(function() {
	 
});



function create(){
	mode = "add";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

	document.getElementById('matk').value = "";
	document.getElementById('tentk').value = "";
	document.getElementById('loaitk').value = "";
	document.getElementById('create_title').innerHTML = "Thêm mới tài khoản";
	document.getElementById('createbutton').value = "Thêm";
}    




function RTFcreate(){
	document.getElementById('create').style.display = 'none';
	document.getElementById('search').style.display = 'block';
	document.getElementById('btnThem').style.display = 'block';
	location.reload();

}    

function edit(pmatk, ptentk){
	mode = "edit";
	oldmatk = pmatk;
	document.getElementById('matk').value = pmatk;
	document.getElementById('tentk').value = ptentk;
	document.getElementById('create_title').innerHTML = "Sửa tài khoản(" + pmatk+ "-"+ ptentk+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';
}    

function editDMDV(pma, pten){
	mode = "edit";
	oldma = pma;
	document.getElementById('madv').value = pma;
	document.getElementById('tendv').value = pten;
	document.getElementById('create_title').innerHTML = "Sửa Chi tiết (" + pma+ "-"+ pten+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';
}    

function editDMVT(pma, pten, pdvt, ptkdt,  ptkvt, ptkgv){
	mode = "edit";
	oldma = pma;
	document.getElementById('mavt').value = pma;
	document.getElementById('tenvt').value = pten;
	document.getElementById('dvt').value = pdvt;
		$('#tk_dt').val(ptkdt);

	$('#tk_gv').val(ptkgv);

	$('#tk_vt').val(ptkvt);

	document.getElementById('create_title').innerHTML = "Sửa Vật tư (" + pma+ "-"+ pten+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';
}    



function refreshDMDV(letter, url)
{
	url = url+ "&&dmdv_initial="+ letter;
	location.href = url;
	return;

}

function refreshDMVT(letter, url)
{
	url = url+ "&&dmvt_initial="+ letter;
	location.href = url;
	return;
}

function SaveDMDV(url){
	var ma=document.getElementById('madv').value;
	var ten=document.getElementById('tendv').value;
	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {madv: ma, tendv: ten, mode: mode, oldma: oldma},
       cache: false,
       success: function(response)
       {
		   if ((response.substring(0,1) == 1))
		   {
				alert(response.substring(2));
		   }
		   else{
			   p = response.indexOf('Duplicate'); 

			   if (p>=0){
				   alert("Mã này đã có rồi!");
			   }
			   else{
				  if (mode == 'add')
				  {
					 document.getElementById('madv').value = "";
					 document.getElementById('tendv').value = "";

				  }
				  else
				   {
					  	location.reload();
				   }
			   }
		   }
       },
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});


}

function SaveDMVT(url){
	var ma=document.getElementById('mavt').value;
	var ten=document.getElementById('tenvt').value;
	var dvt=document.getElementById('dvt').value;
	var tk_vt= $('select[name=tk_vt]').val();
	var tk_gv= $('select[name=tk_gv]').val();
	var tk_dt= $('select[name=tk_dt]').val();

	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {mavt: ma, tenvt: ten, dvt: dvt, tk_vt: tk_vt, tk_gv: tk_gv, tk_dt: tk_dt,  mode: mode, oldma: oldma},
       cache: false,
       success: function(response)
       {
		   if ((response.substring(0,1) == 1))
		   {
				alert(response.substring(2));
		   }
		   else{
			   p = response.indexOf('Duplicate'); 

			   if (p>=0){
				   alert("Mã này đã có rồi!");
			   }
			   else{
				  if (mode == 'add')
				  {
					 document.getElementById('mavt').value = "";
					 document.getElementById('tenvt').value = "";

				  }
				  else
				   {
					  	location.reload();
				   }
			   }
		   }
       },
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});
}


function SaveNew(host, username, password, databaseName, urlupdate ){
	//alert(urlupdate);
	var matk=document.getElementById('matk').value;
	var tentk=document.getElementById('tentk').value;
	//var loaitk=document.getElementById('loaitk').value;

	if (matk == "" || tentk ==  "")
	{
		alert("Mã, Tên tài khoản không được rỗng!");
		return;
	}

	jQuery.ajax({
       type: "POST",
       url: urlupdate,
	   data: {host: host, username: username, password: password, databaseName: databaseName, matk: matk, tentk: tentk,  oldmatk: oldmatk, mode: mode},
       cache: false,
       success: function(response)
       {
		 if (response == "2")
		 {
			 alert("Mã TK này đã có rồi!");
		 }
		 else if (response == "1")
		 {
			 alert("Không có bảng DMTK!");

		 }
		 else if (response == "3")
		 {
			 alert("Co loi du lieu, mau tin khong ghi duoc!");

		 }
		 else 
		   {
			 if (mode == 'add'){
			 document.getElementById('matk').value = "";
			 document.getElementById('tentk').value = "";
			 }
			 else{
				 	location.reload();
			 }

		   }
       },
	
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});
	if (mode == 'edit')
	{
		RTFcreate();
	}


}
